package com.example.myapplication2003.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.myapplication2003.R;

public class ViewPagerAdapter extends PagerAdapter {

    private Context mContext;
    private int[] mImages;
    public ViewPagerAdapter(Context context, int[] images) {
        mContext = context;
        mImages = images;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
    /**
     * 返回页面的数量
     */
    @Override
    public int getCount() {
        return mImages.length;
    }
    /**
     * 渲染每一个页面的内容
     */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        // 加载布局
        View view
                = LayoutInflater.from(mContext).inflate(R.layout.banner_view_item, container, false);
        ImageView imageView = view.findViewById(R.id.image);  // 查找到组件
        imageView.setImageResource(mImages[position]);       // 设置图片
        container.addView(view);      // 添加到根组件
//        TextView textView = new TextView(mContext);
//        textView.setText("this is item " + position);
//        container.addView(textView);      // 添加到根组件
//        Log.i("tag", position + "");



        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull  Object object) {
        // 从根组件中删除子组件
        container.removeView((View)object);
        Log.i("tag", position + " remove");
    }
}
